package com.tabjin.rfidsocket.controller.v1.access;

import com.tabjin.rfidsocket.common.utils.BeanValidator;
import com.tabjin.rfidsocket.common.utils.PagedGridResult;
import com.tabjin.rfidsocket.common.utils.TabJsonResult;
import com.tabjin.rfidsocket.pojo.ArchHouse;
import com.tabjin.rfidsocket.pojo.bo.access.ArchHouseBO;
import com.tabjin.rfidsocket.pojo.vo.access.ArchHouseVO;
import com.tabjin.rfidsocket.service.access.ArchHouseService;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author tabjin
 * create at 2021/1/16 22:55
 * @program rfid_socket
 * @description
 */
@RestController
@RequestMapping(value = "/access/house")
public class HouseController {
    private final ArchHouseService archHouseService;

    public HouseController(ArchHouseService archHouseService) {
        this.archHouseService = archHouseService;
    }

    /**
     * 新增房屋
     *
     * @param archHouseBO
     * @return
     */
    @RequiresPermissions(value = {"/access/house/add"}, logical = Logical.OR)
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public TabJsonResult add(@RequestBody ArchHouseBO archHouseBO) {
        archHouseService.add(archHouseBO);
        return TabJsonResult.ok();
    }

    /**
     * 删除房屋
     *
     * @param houseId
     * @return
     */
    @RequiresPermissions(value = {"/access/house/delete"}, logical = Logical.OR)
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public TabJsonResult delete(@RequestParam(value = "id", required = true) Integer houseId) {
        archHouseService.delete(houseId);
        return TabJsonResult.ok();
    }

    /**
     * 更新房屋
     *
     * @param archHouseBO
     * @return
     */
    @RequiresPermissions(value = {"/access/house/update"}, logical = Logical.OR)
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public TabJsonResult update(@RequestBody ArchHouseBO archHouseBO) {
        ArchHouseVO result = archHouseService.update(archHouseBO);
        return TabJsonResult.ok(result);
    }

    /**
     * 获取所有房屋
     *
     * @return
     */
    @RequiresPermissions(value = {"/access/house/list"}, logical = Logical.OR)
    @RequestMapping(value = "/all", method = RequestMethod.GET)
    @ResponseBody
    public TabJsonResult all(@RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
                             @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize) {
        PagedGridResult result = archHouseService.getAll(page, pageSize);
        return TabJsonResult.ok(result);
    }

    /**
     * @param buildingId
     * @return
     */
    @RequiresPermissions(value = {"/access/house/list"}, logical = Logical.OR)
    @RequestMapping(value = "/by/building", method = RequestMethod.GET)
    @ResponseBody
    public TabJsonResult getHousesByBuildingId(@RequestParam(value = "buildingId") Integer buildingId) {
        List<ArchHouse> result = archHouseService.getHousesByBuildingId(buildingId);
        return TabJsonResult.ok(result);
    }

    /**
     * @param buildingId
     * @return
     */
    @RequiresPermissions(value = {"/access/house/list"}, logical = Logical.OR)
    @RequestMapping(value = "/by/building_id", method = RequestMethod.POST)
    @ResponseBody
    public TabJsonResult getHousesByBuildingUnit(@RequestParam(value = "buildingId") Integer buildingId) {
        List<ArchHouse> result = archHouseService.getHousesByBuildingId(buildingId);
        return TabJsonResult.ok(result);
    }

    /**
     * 获取单个房屋
     *
     * @param houseId
     * @return
     */
    @RequiresPermissions(value = {"/access/house/list"}, logical = Logical.OR)
    @RequestMapping(value = "/item", method = RequestMethod.GET)
    @ResponseBody
    public TabJsonResult item(@RequestParam(value = "id", required = true) Integer houseId) {
        ArchHouseVO item = archHouseService.getItem(houseId);
        return TabJsonResult.ok(item);
    }
}
